<template>
    <div class='home-container'>
        <SideNav :openeds="openeds" :activeTab="activeTab"></SideNav>
        <div class="right-container">
            <Header class="header" :headerList="headerList"></Header>
            <div class="right-content white-color">
                <div class="search-contain">
                    <el-form :inline="true" :model="form" class="demo-form-inline">
                        <el-form-item label="手机号码">
                            <el-input v-model="form.phonenumber" placeholder="手机号码" class="static-input">
                            </el-input>
                        </el-form-item>
                        <el-form-item label="姓名">
                            <el-input v-model="form.fullname" placeholder="姓名" class="static-input">
                            </el-input>
                        </el-form-item>
                        <el-form-item label="班级">
                            <el-select v-model="form.class" placeholder="全部" class="static-input el-select-common">
                                <el-option v-for="item in classList" :key="item.id" :label="item.name" :value="item.id"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="onSubmit">查询</el-button>
                            <el-button @click="resetForm()">重置</el-button>
                        </el-form-item>
                    </el-form>
                </div>
                <div class="content-contain">
                    <el-table
                        v-loading="loading"
                        :data="tableData"
                        style="width: 100%"
                        :header-cell-style="{background:'#EFF1F6',color:'#1C1C1C'}"
                        stripe
                        :row-key='getRowKeys'
                        :expand-row-keys="expands"
                        @expand-change="expandSelect">
                        <el-table-column type="expand">
                            <template slot-scope="scope">
                                <el-form label-position="left" inline class="demo-table-expand" v-if="scope.row.classInfoArr && scope.row.classInfoArr.length">
                                    <!-- 第一行 -->
                                    <el-row :gutter="20" v-for="(item, index) in scope.row.classInfoArr" :key="index">
                                        <el-col :span="5">
                                            <div class="grid-content bg-purple">
                                                <span>
                                                    <img src="../../src/assets/img/welcome/time.png" alt="">
                                                </span>
                                                {{ item.begindate }}
                                            </div>
                                        </el-col>
                                        <el-col :span="6">
                                            <div class="grid-content bg-purple">
                                                <span>
                                                    <img src="../../src/assets/img/welcome/greyBook.png" alt="">
                                                </span>
                                                {{ item.name }}
                                            </div>
                                        </el-col>
                                        <el-col :span="6">
                                            <div class="grid-content bg-purple">
                                                <span>
                                                    <img src="../../src/assets/img/welcome/greyPerson.png" alt="">
                                                </span>
                                                {{ item.lecturername }}
                                            </div>
                                        </el-col>
                                        <el-col :span="5">
                                            <div class="grid-content bg-purple">
                                                <span>
                                                    <img src="../../src/assets/img/welcome/greyPerson.png" alt="">
                                                </span>
                                                {{ item.assistantname }}
                                            </div>
                                        </el-col>
                                        <el-col :span="2">
                                            <div class="grid-content bg-purple">
                                                <span title="删除" class="delete-img" @click="deleteclass(item, scope.row, index)">
                                                    <img src="../../src/assets/img/welcome/delete.png" alt="">
                                                </span>
                                            </div>
                                        </el-col>
                                    </el-row>
                                </el-form>
                                <el-form label-position="left" inline class="demo-table-expand" v-else>
                                    <!-- 第一行 -->
                                    <el-row :gutter="20">
                                        <el-col :span="24">
                                            <div class="grid-content bg-purple" style="text-align: center">
                                                暂无班级
                                            </div>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </template>
                        </el-table-column>
                        <el-table-column label="手机号码" prop="phone" width="160px">
                        </el-table-column>
                        <el-table-column label="姓名" prop="fullname" width="140px">
                        </el-table-column>
                        <el-table-column label="邀请人" prop="inviter" width="140px">
                        </el-table-column>
                        <el-table-column label="出生日期" prop="birthday" width="240px">
                        </el-table-column>
                        <el-table-column label="性别" width="80px">
                            <template slot-scope="scope">
                                <span>
                                    {{scope.row.sex | sexStatus}}
                                </span>
                            </template>
                        </el-table-column>
                        <el-table-column label="备注" prop="desc">
                        </el-table-column>
                        <el-table-column label="操作" width="140px">
                            <template slot-scope="scope">
                                <span class="operate" title="编辑" @click="editRow(scope.row.id)">
                                    <img src="../../src/assets/img/welcome/edit.png" alt="">
                                </span>
                                <span class="operate" title="班级" @click="JumpChooseClass(scope.row.id)">
                                    <img src="../../src/assets/img/welcome/book.png" alt="">
                                </span>
                                <span class="operate" title="删除" @click="deleteTeacherStudent(scope.row.id)">
                                    <img src="../../src/assets/img/welcome/delete.png" alt="">
                                </span>
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
                <div class="footer-contain">
                    <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
                                   :page-sizes="[20, 50, 100, 200]" :page-size="pagesize"
                                   layout="total, sizes, prev, pager, next, jumper" :total="total">
                    </el-pagination>
                    <div class='go-page'>确定</div>
                </div>
            </div>
        </div>

    </div>

</template>

<script>
import SideNav from '../../src/components/SideNav'
import Header from '../../src/components/Header'
import {
    getStudentPage,
    deleteTeacherStudent,
    deleteclass
} from '@/api/studentList'
import {
    getClassDetail,
    getClassList
} from '@/api/classList'
export default {
    components: {
        SideNav,
        Header
    },
    data() {
        return {
            headerList: ['会员', '学生', '学生列表'],
            openeds: ['2', '2-2'],
            activeTab: 'Member',
            form: {
                phonenumber: '',
                fullname: '',
                class: ''
            },
            loading: false,
            page: 1,
            pagesize: 10,
            total: 0,
            tableData: [],
            expands: [],
            classList: []
        }
    },
    filters: {
        sexStatus(val) {
            let type = {
                '1': '男',
                '2': '女'
            }
            return type[val]
        }
    },
    mounted() {
        this.getStudentPage()
        this.getClassList()
    },
    methods: {
        getClassList() {
            getClassList({}).then(res => {
                if (res.code == 1) {
                    this.classList = res.data || []
                }
            })
        },
        getStudentPage() {
            this.loading = true
            getStudentPage({
                page: this.page,
                pagesize: this.pagesize,
                phonenumber: this.form.phonenumber,
                realname: this.form.realname,
                fullname: this.form.fullname,
                class: this.form.class
            }).then(res => {
                this.loading = false
                if (res.code == 1) {
                    this.tableData = res.data.list
                    this.total = res.data.recordcount
                }
            }).catch(e => {
                this.loading = false
            })
        },
        onSubmit() {
            this.getStudentPage()
        },
        resetForm() {
            Object.keys(this.form).forEach(item => {
                this.form[item] = ''
            })
            this.getStudentPage()
        },
        handleSizeChange(val) {
            console.log(`每页 ${val} 条`)
            this.pagesize = val
            this.getStudentPage()
        },
        handleCurrentChange(val) {
            console.log(`当前页: ${val}`)
            this.page = val
            this.getStudentPage()
        },
        handleEdit(index, row) {
            console.log(index, row)
        },
        deleteclass(item, row, index) {
            // console.log(row.classInfoArr)
            this.$confirm('删除，请确认？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                deleteclass({
                    student: row.id,
                    class: item.id
                }).then(res => {
                    if (res.code == 1) {
                        row.classInfoArr.splice(index, 1)
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        })
                    }
                })
            }).catch(() => {

            })
        },
        deleteTeacherStudent(id) {
            this.$confirm('删除，请确认？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                deleteTeacherStudent({
                    student: id
                }).then(res => {
                    if (res.code == 1) {
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        })
                        this.getStudentPage()
                    }
                })
            }).catch(() => {

            })
        },
        handleDelete(index, row) {
            console.log(index, row)
        },
        getRowKeys: function (row) {
            return row.id
        },
        getDetail(id) {
            return new Promise((resolve, reject) => {
                getClassDetail({
                    id: id
                }).then(res => {
                    if (res.code == 1) {
                        let data = res.data
                        resolve(data)
                    }
                })
            })
        },
        expandSelect: async function (row, expanded) {
            var that = this
            let classInfoArr = []
            for (let item of row.classlist) {
                const classInfo = await that.getDetail(item.class) || {}
                classInfoArr.push(classInfo)
            }
            this.$set(row, 'classInfoArr', classInfoArr)
        },
        editRow(id) {
            this.$router.push({
                path: '/addStudent',
                query: {
                    id: id,
                    action: 'edit'
                }
            })
        },
        JumpChooseClass(id) {
            this.$router.push({
                path: '/chooseClass', //跳转的路径
                query: {
                    type: 1,
                    student: id
                }
            })
        },
        deleteRow() {
            this.$confirm('删除，请确认？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {

            }).catch(() => {

            })
        }
    }
}

</script>
<style lang="less" scoped>
    @import url('../../src/assets/less/Mixins.less');
    @import url('../../src/assets/less/common.less');

    html,
    body {
        height: 100%
    }

    .search-contain {
        // margin-top: 20px;
        padding-left: 20px;
        padding-top: 24px;
    }

    .content-contain {
        // padding-left: 16px;
    }

    .right-container {
        background: #ffffff;
        height: 100%;
    }

    .operate {
        cursor: pointer;
        margin-right: 20px;

        img {
            width: 16px;
            height: 16px;
        }
    }

    .grid-content {
        span {
            position: relative;
            margin-right: 5px;
            top: 3px;
        }
    }

    .bg-purple {
        img {
            width: 16px;
            height: 16px;
        }
    }

    .delete-img {
        cursor: pointer;
    }

</style>
<style lang="less">
    .el-table {
        border: 1px solid rgba(223, 230, 236, 1) !important;
        border-bottom: none !important;

        .expanded td {
            box-shadow: 0px 2px 0px 0px rgba(244, 244, 244, 1) !important;
            z-index: 1 !important;
            position: relative !important;
            /*border-bottom: none;*/
        }

        .expanded {
            box-shadow: 0px 2px 0px 0px rgba(244, 244, 244, 1) !important
        }
    }

    .el-table__header-wrapper {
        height: 40px !important;

        .el-table__header {
            td {
                padding: 8px 0px !important;

            }
        }

        background: rgba(239, 241, 246, 1) !important;
    }

    .el-table__header tr,
    .el-table__header th {
        padding: 0 !important;
        height: 40px !important;
    }

    .el-table tbody .el-table__row:hover>td {
        background: rgba(85, 168, 253, 0.05) !important
    }

    .el-table__body-wrapper {
        .el-row {
            margin-bottom: 16px !important;

            &:first-child {
                margin-top: 10px !important;
            }
        }

        tbody {

            .table__row,
            td {
                padding: 8px 0px !important;

            }

            tr {
                &:hover {
                    /*background:rgba(85,168,253,0.05)!important;*/
                }
            }

        }

        .el-table__expanded-cell {
            padding-left: 60px !important;
        }
    }

    .static-input {
        width: 150px !important;
        height: 40px !important;
    }

    .el-form-item__content {
        margin-right: 0px !important;
    }

    // 分页样式
    .el-pagination {
        .el-pager {
            .number {
                margin: 0px !important;
                background: #ffffff !important;
                border: 1px solid rgba(209, 219, 229, 1);
                color: #546374 !important;
                font-weight: 400;
                border-left: none
            }

            li.active {
                background-color: #409EFF !important;
                color: #fff !important;
                border-radius: 0px !important;
                width: 29px !important;
                height: 28px !important;
                border: solid 1px #409EFF !important;
            }

            li {
                border-radius: 0px !important;
            }
        }

        .btn-prev,
        .btn-next {
            margin: 0px !important;
            background: #ffffff !important;
            border: 1px solid rgba(209, 219, 229, 1);

        }

        .btn-prev {
            border-top-right-radius: 0px !important;
            border-bottom-right-radius: 0px !important;
        }

        .btn-next {
            border-top-left-radius: 0px !important;
            border-bottom-left-radius: 0px !important;
        }

        .btn-next {
            border-left: none;
        }
    }

    // 下拉选
    .el-select-dropdown__item.hover,
    .el-select-dropdown__item:hover {
        background-color: #55a8fd !important;
        color: #ffffff !important;
    }

    .el-select-dropdown__list {
        padding-top: 0px !important;
        padding-bottom: 0px !important;
        font-size: 14px !important;
        font-family: PingFang-SC-Regular !important;
        font-weight: 400;
    }

    .popper__arrow {
        display: none !important;
    }

    // 日历
    .el-picker-panel__body {
        font-family: PingFang-SC-Regular !important;
    }

    @import url('../../src/assets/less/override-element-ui.less');

</style>
